package com.education.welco.controller;


import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.education.common.core.BaseController;
import com.education.common.pojo.AjaxResult;
import com.education.common.pojo.CatalogResultVO;
import com.education.common.pojo.OResources;
import com.education.common.pojo.SQuestionsVO;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.StringUtils;
import com.education.welco.feign.wisdomResFeign;
import com.education.welco.pojo.*;
import com.education.welco.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 教师创建课程 信息操作处理
 *
 * @author d
 * @date 2020-05-08
 */
@RestController
@RequestMapping("/sCatalog")
public class SCatalogController extends BaseController {


    @Autowired
    private ISCatalogService catalogService;
    @Autowired
    private wisdomResFeign wisdomResFeign;
    @Autowired
    private ISCatalogMenuService catalogMenuService;
    @Autowired
    private ISResourcesService resourcesService;
    @Autowired
    private ISQuestionsService questionsService;

    @Autowired
    private LShareService lShareService;

    @GetMapping("/findById")
    public ApiReturnObject fingById(int catalogId){
        SCatalog build = SCatalog.builder().parentId(catalogId).build();
        List<SCatalog> sCatalogs = catalogService.selectSCatalogList(build);
        int count = resourcesService.selectresourcesByCatalog(catalogId);
        if (StringUtils.isEmpty(sCatalogs)) return new ApiReturnObject("200","成功","");
        sCatalogs.get(0).setCount(count);
        return new ApiReturnObject("200","成功",sCatalogs.get(0));
    }
    

    /**
     * 根据课程小节的id   查询出资源
     */
    @GetMapping("/list")
    public ApiReturnObject list(SCatalog catalog, Integer userId, Integer pageSize, Integer pageNum) {
        if (pageSize != null && pageNum != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        //根据当前登录老师id 查询出列表
        if (userId != null) {
            catalog.setCatalogTeacherid(userId + "");
            List<SCatalog> list = catalogService.selectSCatalogList(catalog);
            if (list.size() > 8) {
                //取前八条数据
                List<SCatalog> newList = list.subList(0, 8);
                return new ApiReturnObject("200", "查询成功", newList);
            }
            return new ApiReturnObject("200", "查询成功", list);
        } else {
            return new ApiReturnObject("201", "缺少相关参数", "");
        }
    }


    /**
     * 添加
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody SCatalog sCatalog) {
        sCatalog.setCreateTime(new Date());
        catalogService.insertSCatalog(sCatalog);
        return new ApiReturnObject("200", "添加成功", "");
    }


    /**
     * 修改课程
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody SCatalog sCatalog) {
        catalogService.updateSCatalog(sCatalog);
        return new ApiReturnObject("200", "修改成功", "");
    }


    /**
     * 删除消息
     */
    @PostMapping("/del")
    public ApiReturnObject remove(String ids) {
        if (ids != null && !"".equals(ids)) {
            catalogService.deleteSCatalogByIds(ids);
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少相关参数", "");
    }

    /*-------------------------------------------往今软平台同步课程-----------------------------------------------------*/


    /**
     * 同步 选择要同步的课程id集合
     * 往那个目录下面同步
     * 达人币数量
     */
    @PostMapping("/shareCatalog")
    public ApiReturnObject shareCatalog(@RequestBody CatalogResultVO resultVO
//                                        @RequestParam("ids") List<Integer> ids,
//                                        @RequestParam("parentId") Integer parentId,
//                                        @RequestParam("price") Integer price
    ) {
        Integer catalogId = resultVO.getCatalogId();
        Integer price = resultVO.getPrice();
        Integer parentId = resultVO.getParentId();
        if (catalogId != null && parentId != null) {
            //for (Integer id : ids) {
                String paperName = "";
                //获取到课程名称
                SCatalogMenu sCatalog = catalogMenuService.selectSCatalogMenuById(catalogId);
                SCatalogMenu sCatalogMenu = catalogMenuService.selectSCatalogMenuById(sCatalog.getParentId());
                SCatalogMenu sCatalogMenu1 = catalogMenuService.selectSCatalogMenuById(sCatalogMenu.getParentId());
                if (sCatalogMenu1 != null) {
                    paperName = sCatalogMenu1.getName();
                }

                //查询目标目录名称
                String name = wisdomResFeign.selectByMenuName(parentId);
                //添加分享列表
                LShare lShare = new LShare();
                lShare.setCatalogName(paperName);
                lShare.setAtion(name);
                lShare.setPrice(new BigDecimal(price));
                lShare.setRemark("智慧云分享课程到今软平台");
                lShare.setUserId(Integer.parseInt(resultVO.getUserId()));
                lShareService.insertLShare(lShare);
                //获取userId
                String userId = sCatalogMenu1.getUserId() != null ? sCatalogMenu1.getUserId().toString() : "";
                //                      1 教学课件  2 技术文档   4 图片 5 真人实操视频  8虚拟教学 6 3d  7 flash
                //查询其下面的资源类型  教学课件 1  技术文档 3  实操视频 4 实战照片 5  虚拟教学 8，9
                List<SResources> sResources = resourcesService.selectSResourcesByCatalogId(catalogId);
                String[] arr = {"1", "2", "5", "4", "8", "6", "7"};
                List<OResources> resList = Lists.newArrayList();
                for (SResources sResource : sResources) {
                    if (Arrays.asList(arr).contains(sResource.getType())) {
                        OResources oResources = new OResources();
                        //遍历添加
                        BeanUtils.copyProperties(sResource, oResources);
                        resList.add(oResources);
                    }
                }
                ArrayList<SQuestionsVO> questionsList = Lists.newArrayList();
                //试卷表jr_paper
                List<SQuestions> sQuestions = questionsService.selectByCatalogId(catalogId);
                for (SQuestions sQuestion : sQuestions) {
                    SQuestionsVO sQuestionsVO = new SQuestionsVO();
                    BeanUtils.copyProperties(sQuestion, sQuestionsVO);
                    questionsList.add(sQuestionsVO);
                }
                CatalogResultVO catalogResultVO = CatalogResultVO.builder().userId(userId).name(sCatalog.getName()).paperName(paperName)
                        .parentId(parentId).price(price).resList(resList).questionsList(questionsList).ids(new ArrayList<>()).build();
                //调用添加catalog 参数 名称 父级id 达人币数量
                wisdomResFeign.addOther(catalogResultVO);
//            }
            return new ApiReturnObject("200", "同步成功", "");
        }
        return new ApiReturnObject("201", "缺少参数,请选择要分享的章节和分享至的章节", "");
    }


}